class PostsController < ApplicationController
  
  before_filter :authenticate, :except => [:index, :show]
  #layout "two_column"
  
  # GET /posts
  # GET /posts.xml
  def index
        
    params[:page] ? page = params[:page] : page = 1
    
    @posts = Post.paginate(:page => page, :order => "published_at DESC", :conditions => ["published = true and published_at < ?", Time.now])
    
    Post.count_by_months
        
    respond_to do |format|
      format.html { render :layout => "two_column" }
      format.xml  { render :xml => @posts }
      format.atom { }
    end
  end

  # GET /posts/1
  # GET /posts/1.xml
  def show
    
    if params[:slug]
      @post = Post.find(params[:slug])
    else
      @post = Post.find(params[:id])
    end
    
    unless @post
      redirect_to :action => "index" 
      return
    end
    
    @post.increment!(:views)
    
    @comment = Comment.new

    respond_to do |format|
      format.html { render :layout => "two_column" }
      format.xml  { render :xml => @post }
    end
  end

  # GET /posts/new
  # GET /posts/new.xml
  def new
    @post = Post.new

    respond_to do |format|
      format.html 
      format.xml  { render :xml => @post }
    end
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.xml
  def create
    @post = Post.new(params[:post])
    @post.user_id = @user.id

    @asset = Asset.new(params[:asset])    
    if @asset.save
      log_event("Asset created", @asset.id)
    else
      log_event("Asset can not be created", @user.id)
      @post.errors.add_to_base("Can not attach the provided image to this post.")
      render :action => "new"  # We don't support image uploads through the REST API yet so assume it's HTML
      return
    end
    
    respond_to do |format|
      if @post.save
        
        log_event("Post created", @post.id)
        flash[:notice] = 'Post was successfully created.'
        
        format.html { redirect_to(@post) }
        format.xml  { render :xml => @post, :status => :created, :location => @post }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /posts/1
  # PUT /posts/1.xml
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        
        log_event("Post updated", @post.id)
        
        flash[:notice] = 'Post was successfully updated.'
        format.html { redirect_to(@post) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.xml
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    log_event("Post destroyed", @post.id)

    respond_to do |format|
      format.html { redirect_to(posts_url) }
      format.xml  { head :ok }
    end
  end
end
